package org.depsea.oss.config.tenant;

import lombok.extern.slf4j.Slf4j;
import org.depsea.oss.filter.FileUploadFilter;
import org.depsea.oss.strategy.FilenameHandleStrategy;
import org.springframework.context.ApplicationContext;
import org.springframework.lang.NonNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public abstract class AbstractTenantConfiguration {

    private final ApplicationContext applicationContext;

    protected AbstractTenantConfiguration(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    protected FilenameHandleStrategy getFilenameHandleStrategy() {
        try {
            return applicationContext.getBean(FilenameHandleStrategy.class);
        } catch (Exception e) {
            log.warn(e.getMessage());
            return null;
        }
    }

    @NonNull
    protected List<FileUploadFilter> getFilterList() {
        try {
            Map<String, FileUploadFilter> filters = applicationContext.getBeansOfType(FileUploadFilter.class);
            return new ArrayList<>(filters.values());
        } catch (Exception e) {
            log.warn(e.getMessage());
            return List.of();
        }
    }
}
